package com.xyhy.core.base.enums;

import com.xyhy.core.base.model.vo.EnumVO;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

public interface BaseEnum {
  String name();

  String getText();

  static String getText(BaseEnum item) {
    return Optional.ofNullable(item).map(x -> x.getText()).orElse(null);
  }

  static <T extends Enum<?>> T getByName(T[] items, String name) {
    if (Objects.isNull(name)) return null;
    return Arrays.stream(items).filter(x -> x.name().equals(name)).findFirst().orElse(null);
  }

  static <T extends BaseEnum> T getByText(T[] items, String text) {
    return Arrays.stream(items).filter(x -> x.getText().equals(text)).findFirst().orElse(null);
  }

  static <T extends BaseEnum> List<EnumVO> getAllVO(T[] items) {
    return Arrays.stream(items).map(x -> new EnumVO().setName(x.name()).setText(x.getText()))
            .collect(Collectors.toList());
  }

}
